package com.hm.manage.pach;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class GetRequestExample2 {
    public static void main(String[] args) {
        String apiUrl = "http://psvs.szpt.ga/api/systemArchive/archiveDetail/320412010001202504281602400000001";
        String param1 = "320412010001202504281602400000001";

        try {
            URL url = new URL(apiUrl);

            // 2. 打开连接并设置基础属性
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000); // 连接超时 5 秒
            conn.setReadTimeout(10000);    // 读取超时 10 秒

            // 3. 设置请求头（可选）
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Accept-Encoding","gzip, deflate");
            conn.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
            conn.setRequestProperty("Connection","keep-alive");
            conn.setRequestProperty("Cookie", "JSESSIONID=0188F746F8021BF407DF3D6AD61C81C8");
            conn.setRequestProperty("Host","psvs.szpt.ga");
            conn.setRequestProperty("Referer","http://psvs.szpt.ga/");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token","2025bea9876d454598128d2d2303375d");


            // 4. 获取响应状态码
            int status = conn.getResponseCode();
            System.out.println("HTTP 状态码: " + status);

            // 5. 读取响应内容
            String response;
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                                    conn.getInputStream(),
                            StandardCharsets.UTF_8))) {
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                response = sb.toString();
            }

            System.out.println("响应内容:\n" + response);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}